import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

import store from "@/store/index.js";

//配置路由规则
const routes = [
  //重定向
  {
    path: "/",
    redirect: "/Home",
  },
  //首页路由
  {
    path: "/Home",
    name: "Home",
    meta: {
      showTab: true,
    },
    component: () => import("@/views/Home/Home.vue"),
  },
  //分类路由
  {
    path: "/Category",
    name: "Category",
    meta: {
      showTab: true,
    },
    component: () => import("@/views/Category/Category.vue"),
  },
  //购物车路由
  {
    path: "/ShoppingCart",
    name: "ShoppingCart",
    meta: {
      showTab: true,
    },
    component: () => import("@/views/ShoppingCart/ShoppingCart.vue"),
  },
  // 个人中心路由
  {
    path: "/Profile",
    name: "Profile",
    meta: {
      showTab: true,
      auth: false,
    },
    component: () => import("@/views/Profile/Profile.vue"),
  },
  // 商品详情路由
  {
    path: "/GoodsDetail",
    name: "GoodsDetail",
    component: () => import("@/views/GoodsDetail/GoodsDetail.vue"),
  },
  // 搜索页
  {
    path: "/Search",
    name: "Search",
    component: () => import("@/views/Search/Search.vue"),
  },
  // 登陆
  {
    path: "/Login",
    name: "Login",
    component: () => import("@/views/User/Login.vue"),
  },
  // 注册
  {
    path: "/Register",
    name: "Register",
    component: () => import("@/views/User/Register.vue"),
  },
  // 用户信息
  {
    path: "/userInfo",
    name: "userInfo",
    component: () => import("@/views/User/UserInfo.vue"),
  },
  // 用户信息变更
  {
    path: "/InfoUpdate",
    name: "InfoUpdate",
    component: () => import("@/views/User/UserInfoChildren/InfoUpdate.vue"),
  },
  // 密码变更
  {
    path: "/PasswordUpdate",
    name: "PasswordUpdate",
    component: () => import("@/views/User/UserInfoChildren/PasswordUpdate.vue"),
  },
  // 我的收藏
  {
    path: "/Collect",
    name: "Collect",
    component: () => import("@/views/Profile/ProfileChildren/Collect.vue"),
  },
  // 联系人地址
  {
    path: "/Contact",
    name: "Contact",
    component: () => import("@/views/Profile/ProfileChildren/Contact.vue"),
  },
  // 联系人地址编辑
  {
    path: "/ContactEdit",
    name: "ContactEdit",
    component: () => import("@/views/Profile/ProfileChildren/ContactEdit.vue"),
  },
 
  // 订单确认
  {
    path: "/OrderCreate",
    name: "OrderCreate",
    component: () => import("@/views/Order/OrderCreate.vue"),
  },
  // 我的订单列表
  {
    path: "/OrderList",
    name: "OrderList",
    component: () => import("@/views/Profile/ProfileChildren/OrderList.vue"),
  },
  // 历史足迹
  {
    path: "/History",
    name: "History",
    component: () => import("@/views/Profile/ProfileChildren/History.vue"),
  },
  // 品牌列表
  {
    path: "/Brand",
    name: "Brand",
    component: () => import("@/views/Home/HomeChildren/Brand.vue"),
  },
  // 立即评价
  {
    path: "/Comment",
    name: "Comment",
    component: () => import("@/views/Profile/ProfileChildren/Comment.vue"),
  },
 
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.name === "Profile" && !store.state.userInfo) {
    // next({path:'/Login'});
    router.push({ path: '/Login' });
  } else {
    next();
  }
});

export default router;
